﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Text;

namespace ListLimiter.ControlTemplates.ListLimiter
{
    public partial class ListLimiterStatus : UserControl
    {
        public SPWeb Web
        {
            get
            {
                return SPContext.Current.Web;
            }
        }

        public SPList List
        {
            get
            {
                return SPContext.Current.List;
            }
        }

        public bool CurrentContextIsList
        {
            get
            {
                return this.List != null;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            base.CreateChildControls();
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            ScriptLink.RegisterScriptAfterUI(this.Page, "core.js", true, false);
            ScriptLink.RegisterScriptAfterUI(this.Page, "CUI.js", false, true);
            ScriptLink.RegisterScriptAfterUI(this.Page, "SP.js", false, true);

            if (this.CurrentContextIsList)
            {
                bool limitEnabled = Common.GetLimitEnabled(this.Web, this.List.ID);
                bool statusEnabled = Common.GetLimitStatusEnabled(this.Web, this.List.ID);

                if (statusEnabled && limitEnabled)
                {
                    ShowStatus();
                }
            }
        }

        private void ShowStatus()
        {
            int limit = Common.GetLimit(this.Web, this.List.ID);
            int limitWarning = Common.GetLimitWarning(this.Web, this.List.ID);
            int itemCount = this.List.ItemCount;


            string script = BuildScript(itemCount, limitWarning, limit);

            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "showstatus", script.ToString(), true);
        }

        private string BuildScript(int itemCount, int limitWarning, int limit)
        {
            string statusColor = GetStatusColor(itemCount, limitWarning, limit);
            string statusMessage = GetStatusMessage(itemCount, limit);
            string statusTitle = GetStatusTitle(limit);

            StringBuilder script = new StringBuilder();

            script.Append("SP.SOD.executeOrDelayUntilScriptLoaded(addListLimitStatus, 'SP.js');");
            script.Append("function addListLimitStatus()");
            script.Append("{");
            script.AppendFormat("var statusId = SP.UI.Status.addStatus('{0}', '{1}', true); ", statusTitle, statusMessage);
            script.AppendFormat("setStatusPriColor(statusId, '{0}');", statusColor);
            script.Append("}");

            return script.ToString();
        }

        private string GetStatusTitle(int limit)
        {
            string statusTitle = Common.GetLimitStatusTitle(this.Web, this.List.ID, true);

            if (string.IsNullOrEmpty(statusTitle))
            {
                string statusTitleFormat = Common.GetLocalizedString(this.Web, "Status_Title");
                statusTitle = string.Format(statusTitleFormat, limit.ToString());
            }

            return statusTitle;
        }

        private string GetStatusMessage(int itemCount, int limit)
        {
            string statusMessage = Common.GetLimitStatusText(this.Web, this.List.ID, true);

            if (string.IsNullOrEmpty(statusMessage))
            {
                string statusMessageFormat = Common.GetLocalizedString(this.Web, "Status_Message");
                statusMessage = string.Format(statusMessageFormat, itemCount, limit);
            }

            return statusMessage;
        }

        private string GetStatusColor(int itemCount, int limitWarning, int limit)
        {
            string statusColor;
            if (itemCount <= limitWarning)
            {
                statusColor = "blue";
            }
            else if (itemCount > limitWarning && itemCount < limit)
            {
                statusColor = "yellow";
            }
            else
            {
                statusColor = "red";
            }
            return statusColor;
        }

    }
}
